﻿using BasicData.Infrastrueturc.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BasicData.Infrastrueturc.Implement
{
    /// <summary>
    /// 基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseRepsoitory<T> : IBaseReposiotry<T> where T : class
    {
        public MyDbContext DB
        {
            get { return Dbcontext; }
        }
        /// <summary>
        /// 上下文
        /// </summary>
        private readonly MyDbContext Dbcontext;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="Dbcontext">上下文</param>
        public BaseRepsoitory(MyDbContext Dbcontext)
        {
            this.Dbcontext = Dbcontext;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t">实体</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> AddAsync(T t)
        {
            Dbcontext.Set<T>().Add(t);
            return await Dbcontext.SaveChangesAsync();
        }

        /// <summary>
        /// 获取全部
        /// </summary>
        /// <returns>返回集合</returns>
        public IQueryable<T> GetAll()
        {
           return Dbcontext.Set<T>().AsQueryable();
            
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="t">实体</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> Update(T t)
        {
            Dbcontext.Set<T>().Update(t);
            return await Dbcontext.SaveChangesAsync();
        }
    }
}
